package cn.icanci.loopstack.ddk.web.controller.config;

import cn.icanci.loopstack.ddk.admin.biz.service.AppConfigService;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.TextValue;
import cn.icanci.loopstack.ddk.common.model.config.AppConfigVO;
import cn.icanci.loopstack.ddk.common.result.R;
import cn.icanci.loopstack.ddk.web.form.AppConfigQueryForm;
import cn.icanci.loopstack.ddk.web.mapping.AppConfigWebMapping;
import cn.icanci.loopstack.ddk.web.model.AppConfig;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/06 21:11
 */
@RestController
@RequestMapping("/ddk/appConfig")
public class AppConfigController {
    @Resource
    private AppConfigService    appConfigService;

    @Resource
    private AppConfigWebMapping appConfigWebMapping;

    @PostMapping("query")
    public R query(@RequestBody AppConfigQueryForm form) {
        PageList<AppConfigVO> queryPage = appConfigService.queryPage(appConfigWebMapping.web2vo(form.getAppConfig()), form.getPaginator().getCurrentPage(),
            form.getPaginator().getPageSize());

        PageList<AppConfig> ret = new PageList<>(appConfigWebMapping.vos2webs(queryPage.getData()), queryPage.getPaginator());

        return R.builderOk().data("queryPage", ret).build();
    }

    @PostMapping("save")
    public R save(@RequestBody AppConfig appConfig) {
        appConfigService.save(appConfigWebMapping.web2vo(appConfig));
        return R.builderOk().build();
    }

    @GetMapping("validateAppConfigName/{appConfigName:.*}")
    public R validateAppConfigName(@PathVariable("appConfigName") String appConfigName) {
        AppConfigVO appConfigVO = appConfigService.queryByAppConfigName(appConfigName);
        return R.builderOk().data("result", appConfigVO == null).build();
    }

    @GetMapping("loadSelector")
    public R loadSelector() {
        List<TextValue> textValues = appConfigService.loadSelector();
        return R.builderOk().data("textValues", textValues).build();
    }
}
